//
//  
//  lvyou
//
//  Created by lanou3g on 13-12-30.
//  Copyright (c) 2013年 lanou3g. All rights reserved.
//

#import "LocationMgr.h"

@implementation LocationMgr

SYNTHESIZE_SINGLETON(LocationMgr)

-(id)init
{
    if ((self = [super init]))
    {
        self.locateStatusType = eSLocateStatusTypeInit;
    }
    return self;
}

-(void)dealloc
{
    if(self.locationManager != nil)
	{
		[self.locationManager stopUpdatingLocation];
		
		// 解决某些破解系统的bug
		if([self.locationManager respondsToSelector:@selector(stopMonitoringSignificantLocationChanges)] == YES)
		{
			[self.locationManager stopMonitoringSignificantLocationChanges];
		}
		
		[self.locationManager setDelegate:nil];
	}
}

//定位
-(void)getPosition:(LocationBlock)block {
    self.locationBlock = block;
    
    if((self.locateStatusType == eSLocateStatusTypeInit) ||
       (self.locateStatusType == eSLocateStatusTypeFailure))
    {
        // 设置搜索标志
        self.locateStatusType = eSLocateStatusTypeInProcess;
        if (self.locationBlock) {
            self.locationBlock(self.locateStatusType, nil);
        }
        
        // 进行定位并且搜索
        BOOL locationServicesEnabled = NO;
        
        locationServicesEnabled = [CLLocationManager locationServicesEnabled];
        
        // 定位服务不可用
        if(locationServicesEnabled == NO){
            self.locateStatusType = eSLocateStatusTypeFailure;
            if (self.locationBlock) {
                self.locationBlock(self.locateStatusType, nil);
            }
        }
        // 定位服务可用
        else
        {
            // 开启定位
            if(self.locationManager == nil)
            {
                CLLocationManager *locationManagerTmp = [[CLLocationManager alloc] init];
                self.locationManager = locationManagerTmp;
                if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
                {
                    [self.locationManager requestWhenInUseAuthorization];
                }
            }
            
            [self.locationManager setDelegate:self];
            [self.locationManager startUpdatingLocation];
        }
    }

}


// =======================================================================
#pragma mark - CLLocationManagerDelegate的代理函数
// =======================================================================
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
	
	// 时间是否在有效范围
    if(locationAge > 5.0)
	{
		return;
	}
	
    // 经纬度是否有效果
    if(newLocation.horizontalAccuracy < 0)
	{
		return;
	}
	
    // 保存经纬度
	[self.locationManager stopUpdatingLocation];
    _location = newLocation;
    
    if(_location!=nil){
        
        self.locateStatusType = eSLocateStatusTypeSuccess;

        if (self.locationBlock) {
            self.locationBlock(self.locateStatusType, self.location);
        }
    }
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    // 停止定位服务
    [self.locationManager stopUpdatingLocation];
    
    self.locateStatusType = eSLocateStatusTypeFailure;

    if (self.locationBlock) {
        self.locationBlock(self.locateStatusType, nil);
    }
}


@end
